package com.youlu.campus.service.activity.impl;

import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.ActivityTaskRule;
import com.youlu.campus.entity.VO.req.CopyTaskRuleReq;
import com.youlu.campus.service.activity.ActivityTaskRuleService;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

@Slf4j
@Service
public class ActivityTaskRuleServiceImpl implements ActivityTaskRuleService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private MapperFacade mapperFacade;

    @Override
    public boolean create(ActivityTaskRule req) {
        req.setCreatedTime(new Date());
        req.setId(null);
        mongoTemplate.insert(req);
        return true;
    }

    @Override
    public boolean update(ActivityTaskRule req) {
        return false;
    }

    @Override
    public ActivityTaskRule find(String activityId, String taskType) {
        log.info(":>>> 开始查找任务规则:{},{}", activityId, taskType);
        if (StringUtils.isBlank(activityId) || StringUtils.isBlank(taskType)) {
            log.error(":>>> 查找任务规则 错误:活动ID或任务类型为空");
            throw new BusinessException("查找任务规则 错误:活动ID或任务类型为空");
        }
        Query query = new Query().addCriteria(Criteria.where("activityId").is(activityId).and("bizType").is(taskType));
        ActivityTaskRule rule = mongoTemplate.findOne(query, ActivityTaskRule.class);
        return rule;
    }

    @Override
    public ActivityTaskRule find(String taskId) {
        Query query = new Query().addCriteria(Criteria.where("taskId").is(taskId));
        ActivityTaskRule rule = mongoTemplate.findOne(query, ActivityTaskRule.class);
        return rule;
    }

    @Override
    public void saveOrUpdateQa(String activityId, Integer point) {
        Query query = new Query().addCriteria(Criteria.where("activityId").is(activityId).and("bizType").is("QA"));
        if (Objects.isNull(point) || point == 0) {
            this.mongoTemplate.remove(query, ActivityTaskRule.class);
            return;
        }
        FindAndModifyOptions findAndModifyOptions = new FindAndModifyOptions();
        findAndModifyOptions.returnNew(Boolean.TRUE);
        findAndModifyOptions.upsert(Boolean.TRUE);
        Date now = new Date();
        Update update = new Update();
        update.set("threshold", 1);
        update.set("points", point);
        update.set("createdTime", now);
        update.set("updatedTime", now);
        this.mongoTemplate.findAndModify(query, update, findAndModifyOptions, ActivityTaskRule.class);
    }

    @Override
    public void doCopyTaskRule(CopyTaskRuleReq copyTaskRuleReq) {
        String copyActivityId = copyTaskRuleReq.getCopyActivityId();
        String copyTaskId = copyTaskRuleReq.getCopyTaskId();
        String bizType = copyTaskRuleReq.getBizType();
        String targetCopyActivityId = copyTaskRuleReq.getTargetCopyActivityId();
        String targetCopyTaskId = copyTaskRuleReq.getTargetCopyTaskId();
        log.info("【copy 活动】开始复制活动任务规则 ->{} taskId ->{}", copyActivityId, copyTaskId);
        Query query = new Query();
        query.addCriteria(Criteria.where("activityId").is(copyActivityId));
        query.addCriteria(Criteria.where("bizType").is(bizType));
        ActivityTaskRule activityTaskRule = this.mongoTemplate.findOne(query, ActivityTaskRule.class);
        if (Objects.isNull(activityTaskRule)) {
            log.error("【copy 活动】结束复制活动任务规则 ->{} bizType ->{}", copyActivityId, bizType);
            return;
        }
        Date now = new Date();
        ActivityTaskRule targetActivityTaskRule = this.mapperFacade.map(activityTaskRule, ActivityTaskRule.class);
        targetActivityTaskRule.setId(null);
        targetActivityTaskRule.setActivityId(targetCopyActivityId);
        targetActivityTaskRule.setTaskId(targetCopyTaskId);
        targetActivityTaskRule.setCreatedTime(now);
        targetActivityTaskRule.setUpdatedTime(now);
        this.mongoTemplate.save(targetActivityTaskRule);
        log.info("【copy 活动】结束复制活动任务规则 ->{} taskId ->{}", copyActivityId, copyTaskId);
    }
}
